﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmTimKiemDoiTraHang : Form
    {
        private PhieuDoiHangManagement phieudoihangMgt = new PhieuDoiHangManagement();
        private PhieuHangTraManagement phieuhangtraMgt = new PhieuHangTraManagement();
        private CTPhieuDoiHangManagement ctphieudoihangMgt = new CTPhieuDoiHangManagement();
        private CTPhieuHangTraManagement ctphieuhangtraMgt = new CTPhieuHangTraManagement();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmTimKiemDoiTraHang(NhanVien nhanvien)
        {
            InitializeComponent();
            this.NhanVien = nhanvien;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            ResultObj result = new ResultObj();            
            dgvTimKiem.AutoGenerateColumns = false;

            if (rdbPDH.Checked)
            {
                result = phieudoihangMgt.GetAllPhieuDoiHang();
                List<PhieuDoiHang> lstPhieuDoiHang = (from pdh in (List<PhieuDoiHang>)result.Result
                                                      where (pdh.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                            pdh.NguoiLapPhieu.ToLower().Contains(txtTimKiem.Text.ToLower())) &&
                                                            pdh.NgayThang.Date >= dtTuNgay.Date && pdh.NgayThang.Date <= dtDenNgay.Date &&
                                                            pdh.IsDelete==0
                                                      select pdh).ToList();
                
                dgvTimKiem.DataSource = lstPhieuDoiHang;                
            }
            else if (rdbPHT.Checked)
            {
                result = phieuhangtraMgt.GetAllPhieuHangTra();
                List<PhieuHangTra> lstPhieuHangTra = (from pht in (List<PhieuHangTra>)result.Result
                                                      where (pht.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                            pht.NguoiLapPhieu.ToLower().Contains(txtTimKiem.Text.ToLower())) &&
                                                            pht.NgayThang.Date >= dtTuNgay.Date && pht.NgayThang.Date <= dtDenNgay.Date &&
                                                            pht.IsDelete==0
                                                      select pht).ToList();

                dgvTimKiem.DataSource = lstPhieuHangTra;              
            }
            else
            {
                MessageBox.Show("Vui lòng check Phiếu đổi hàng hoặc Phiếu Hàng trả", "Thông báo");
                return;
            }
        }

        private void dgvTimKiem_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvTimKiem.CurrentRow.Cells[1].Value.ToString().Contains("PD"))
            {
                PhieuDoiHang phieudoihang = (PhieuDoiHang)dgvTimKiem.CurrentRow.DataBoundItem;

                FrmPhieuDoiHang frmPhieuDoiHang = new FrmPhieuDoiHang(phieudoihang);
                dgvTimKiem.DataSource = null;
                this.Hide();
                frmPhieuDoiHang.ShowDialog();
                this.Show();
                
            }
            else if (dgvTimKiem.CurrentRow.Cells[1].Value.ToString().Contains("PHT"))
            {
                PhieuHangTra phieuhangtra = (PhieuHangTra)dgvTimKiem.CurrentRow.DataBoundItem;

                FrmPhieuHangTra frmPhieuHangTra = new FrmPhieuHangTra(phieuhangtra);
                this.Hide();
                dgvTimKiem.DataSource = null;
                frmPhieuHangTra.ShowDialog();
                this.Show();       
            }
        }

        private void btmThem_Click(object sender, EventArgs e)
        {
                FrmDoiTraHang frmDoiTraHang = new FrmDoiTraHang();
                frmDoiTraHang.NhanVien = this.nhanvien;
                this.Hide();
                frmDoiTraHang.ShowDialog();
                this.Show();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
